const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const {
  Server
} = require("socket.io");
const {
  asyncHandler
} = require("./getSendResult")
const {
  whetherToPay
} = require("../services/CurrServ")

const io = new Server(server, {
  cors: {
    origin: ['http://localhost:5173'] // 配置客户端可跨域地址
  }
});

app.get("/queryOrder", asyncHandler(async (req, res) => {
  io.on('connection', (socket) => {
    const timer = setInterval(async function () {
      await whetherToPay(req.query.Order, (state) => {
        socket.emit("test", state)
      })
    }, 1000)

    socket.on("disconnect", () => {
      clearInterval(timer)
    })

  })
  return ""
}))



server.listen(8081, () => {
  console.log('server listen on 8081');
});